package org.kisekiproject.instance {
	import flash.utils.Dictionary;
	
	import org.kisekiproject.classlibrary.IClassLibrary;

	/**
	 * Use a class library as definition library.
	 */
	public class ClassDefinitionLibrary implements IDefinitionLibrary {

		private var _classLibrary:IClassLibrary;
		private var _definitions : Dictionary;

		/**
		 * Constructor.
		 */
		public function ClassDefinitionLibrary(classLibrary:IClassLibrary):void {
			_definitions=new Dictionary();
			_classLibrary=classLibrary;
		}

		/**
		 * Get definition.
		 */
		public function getDefinition(type:String):InstanceDefinition {
			if (_definitions[type])
				return InstanceDefinition(_definitions[type]);

			var cls:Class=_classLibrary.getClass(type);

			if (!cls)
				throw new Error("Definition does not exist: "+type);

			_definitions[type]=new InstanceDefinition(cls);

			return _definitions[type];
		}
	}
}